home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / sources.arc / DANG_SRC.LZH / GEN_ESTB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-26  |  12.7 KB  |  432 lines

  1. /* general estab. routine */
  2.  
  3. char *lookup_backpack();
  4. #include <globals2.h> 
  5. #include <stdio.h>
  6. #include <strings.h>
  7. #define    Bconstat(a)    (int)bios(1,a)
  8. extern char *screen,*temp;
  9. extern char parry[];                 /* that -q disables intro mssg on play.ttp */
  10.  
  11. char welcome[]="2 welco"; /* Welcome to my shoppe! */
  12.    
  13. /* loads up items,prices from a datafile. All estabs will use
  14.    this, but they will user diff data files. The form of the datafile 
  15.    is:
  16.        item_name
  17.        price
  18.  
  19.    where item_name and price must be < 14 chars in length! 
  20.  
  21.   max_number controls which items are displayed. ie if you only want 
  22.   the character to be able to buy items 1-5 set max_number =5  
  23.  
  24.   ** also loads up a picture of a smithy and animates it.
  25.      
  26. */
  27.  
  28. #define max_number 45          /* max number of items avail to buy*/
  29. /* read in price list and item names */
  30.   /* place them in an array of structures */
  31.      struct item_list
  32.       {
  33.        char name[30];
  34.        int  price;
  35.       }items[max_number]; 
  36.    
  37.  
  38.  
  39. /**********************/
  40. /* name: gen_estab(mfs,mfd,a,b,c)
  41.  
  42.    mfs= MFDB source.
  43.    mfd= MFDB dest.
  44.    a = filename to read from disk( file holds text info/prices )
  45.    b = name of establishment eq: Tail of the Dog Tavern
  46.    c = if 1 then it's an armory!
  47.           2 its the supply shoppe
  48. */
  49. gen_estab(mf_src,mf_dest,input_file,name,type_of_estab)    /* replace w/ gen_estab */
  50. register MFDB *mf_src,*mf_dest;   /* the src and dest MFDBs!! */
  51. char *input_file,*name,type_of_estab;
  52. {
  53.  
  54.   FILE *ifp;
  55.   char array[20]; /* holds input data. max of 14 chars + null */
  56.   int number;     /* holds converted number */
  57.   int count=0; /*counts actually how many items are avail to buy */
  58.   int result_returned;
  59.   char com2;
  60.   int type=0;   /* if 0 store item name */
  61.                 /* if 1 store price */
  62.   int max_weapons_avail=43;       /* a random number */
  63.   int rectangle_[8];  /* 0-3 is source. 4-7 is the dest */
  64.   int count22=1,dir_of_ani=1;
  65.   int first = 1;
  66.  
  67.  
  68.   /** set up dest rectangle */
  69.   rectangle_[4] = 204; rectangle_[5] = 32;
  70.   rectangle_[6] = 233; rectangle_[7] = 62;
  71.   /* set first source rectangle */
  72.   rectangle_[0] = 9;  rectangle_[1] = 12;
  73.   rectangle_[2] = 39; rectangle_[3] = 42;
  74.  
  75.  
  76.    ifp = fopen(input_file,"r");
  77.     if( ifp == NULL) {printf("\nError opening %s--\n",input_file); scanf("%c",&com2);exit(1);}
  78.      while(  ((fgets(array,15,ifp)) != NULL) && count < max_weapons_avail) 
  79.       /* while not reached eof or reached max # of items you can buy..
  80.          the code : count < max_weapons_avail makes sure you dont go out 
  81.          of bounds in the array items[..] */
  82.       { 
  83.         if (type == 0)  /* process Name of item */ 
  84.             strcpy(items[count].name,array);    
  85.             items[count].name[13] = '\0';      /* kill \n */ 
  86.         if(type == 1)  /* process the Price of the item */
  87.             items[count].price = atoi(array);  /*conv to integer */ 
  88.  
  89.          type++;
  90.          if(type >1){    /*if name and price read in, reset counter
  91.                             and goto next cell in array to process
  92.                             next item */ 
  93.                       type = 0;
  94.                       count++;          /* number of items loaded */
  95.                     }
  96.     
  97.    }  /*end of while*/
  98.  fclose(ifp);
  99.  
  100.  max_weapons_avail = rnd(count);  /* randomly have some items there at any time */
  101.  
  102. max_weapons_avail++;  
  103.   clear_it();
  104.  
  105.     v_gtext(handle,6,11,"Welcome Friend to my");
  106.     v_gtext(handle,6,19,"humble shoppe...");
  107.     v_gtext(handle,6,35,"What may I do for");
  108.     v_gtext(handle,6,43,"you?");
  109.     v_gtext(handle,6,59,"B)uy ");
  110.     v_gtext(handle,6,67,"S)ell");
  111.     v_gtext(handle,6,75,"E)xit");
  112.  
  113. invoke_tsr( welcome );
  114.  
  115. if(type_of_estab != 2 )  /* if not supply....animate! */
  116.   {
  117.    while( !(Bconstat(2)) )
  118.     {
  119.      Vsync();
  120.      vro_cpyfm(handle,3,rectangle_,mf_src,mf_dest);
  121.  
  122.       
  123.      rectangle_[1] +=(33*dir_of_ani);    /* update new source rects. only need to
  124.                               update the y values. */
  125.      rectangle_[3] +=(33*dir_of_ani);  
  126.      if( count22 == 4 )               /* then change the direction of animation */
  127.       {dir_of_ani = dir_of_ani * -1; count22=1; 
  128.       }  /* change dir and reset counter */ 
  129.       if(dir_of_ani == 1 && count22 == 2 ) 
  130.             { if(first ==0) invoke_tsr( parry );
  131.               first = 0;
  132.             } 
  133.       count22++;
  134.      short_wait();
  135.   }/* end of while */
  136. } /* end of animate... */
  137.  
  138.  com2 = Bconin(2);  /* get the input */
  139.  
  140. clear_it();
  141.  
  142. if ( com2 == 'B' || com2 =='b' )
  143.  buy(max_weapons_avail,type_of_estab);
  144.  
  145. if(type_of_estab == 2 && (com2 == 'S' || com2 == 's'))
  146. {
  147. v_gtext(handle,6,11,"Sorry..I dont buy used");
  148. v_gtext(handle,6,19,"Armour...             ");
  149. }
  150.  
  151. if(type_of_estab != 2)
  152. {
  153.  if ( com2 == 'S' || com2 == 's')
  154.  sell(type_of_estab);
  155. }
  156.  
  157. }
  158.  
  159.  
  160. /*********************************************************/
  161. sell(type_of_estab)
  162. int type_of_estab;
  163. {
  164.  int price;
  165.  char s,t;
  166.  
  167.  
  168. if ( type_of_estab == 1  )
  169.       /* if armory AND you have some armor, can only buy current armour*/
  170. {
  171.  if (strcmp(user.armor,"None") !=0)
  172.   {
  173.     v_gtext(handle,6,11,"I'll give you");
  174.     price = user.armor_num * 20 + user.con;
  175.     sprintf(string,"%d gp for it",price);
  176.     v_gtext(handle,6,19,string,184,0,1);
  177.     v_gtext(handle,6,27,"Accept [y/n]");
  178.     t = Bconin(2);
  179.   if( t == 'y' || t == 'Y')
  180.   {
  181.    user.gold +=price;
  182.    user.ac = 18;      /* set ac to easiest to hit! */
  183.    user.armor_num = 0;  /* set armor_num to 0, ts gone now.. */
  184.    strcpy(user.armor,"None         ");
  185.   }
  186.  }/*end of if you have some armor */
  187. }/* end of if in armory */
  188. else
  189. {
  190. view_backpack();
  191.  
  192. do
  193.  { 
  194.  v_justified(handle,6,83,"# to sell? [X] to exit",184,0,1);
  195.  s = Bconin(2);
  196.  if( s == 'x' || s== 'X' ) return(1);
  197.  }while( (s-'0' < 0 || s-'0' >6) || (user.backpack[s - '0'] == -1));        /* if you dont own it, can sell it! */
  198.   
  199.    price =abs( unique_item[user.backpack[s-'0']].points);
  200.    price = price * user.lvl;
  201.    price += user.con; /* if your good looking add some gp */
  202.    clear_it();
  203.  v_gtext(handle,6,11,"I'll give you");
  204.  sprintf(string,"%d gp for it",price);
  205.  v_gtext(handle,6,19,string,184,0,1);
  206.  v_gtext(handle,6,27,"Accept [y/n]");
  207.  t = Bconin(2);
  208.  if( t == 'y' || t == 'Y')
  209.  {
  210.   user.gold = user.gold + price;
  211.   user.backpack[s-'0'] = -1;  /* set it to -1, its gone now.. */
  212.  }
  213. }
  214.  
  215. }/* fin */
  216.  
  217.  
  218. /******************************************/
  219.  
  220. buy(max_weapons_avail,type_of_estab)
  221. int max_weapons_avail,type_of_estab;  
  222. {
  223.    
  224.   int zz=11;
  225.   char com1;
  226.   int full=-1; /* for each increment to this var, we know the offset
  227.                  to the weapon numbers is full * 7
  228.                  we need to start at -1!! */
  229.   int type=0;   /* if 0 store item name */
  230.                 /* if 1 store price */
  231.   int not_complete = 1; 
  232.  
  233.    
  234.  
  235.    clear_it();
  236.    for(type=0;type<max_weapons_avail;type++)
  237. {
  238.    not_complete =1;
  239.    display_wares(type%7,type,&zz);
  240.    if( zz == 67 || type == max_weapons_avail-1 ) /* if output window full
  241.                                              or reached end of list..then*/
  242.     { 
  243.  
  244.       full++; /* increment this */
  245.      if( (type - max_weapons_avail+1) == 0 ) /* if at last item to buy, then
  246.                                          use different text */
  247.         v_justified(handle,6,zz,"# to buy or E to exit",184,0,1);
  248.      else
  249.       {
  250.       v_justified(handle,6,zz,"# to buy,C to continue",184,0,1);
  251.       v_justified(handle,6,zz+8,"or E to exit:",184,0,1);
  252.       }
  253.        zz =11; /*reset y position */
  254.        
  255.     do
  256.      {
  257.      com1 = Bconin(2);          /* get input */
  258.        if(   (com1-'0') < 7  && (com1 -'0') > -1  ) 
  259.           { clear_it(); 
  260.             process_the_buy(com1-'0',full*7,type_of_estab);  /* send offset */
  261.             not_complete = 0;       /* set flag false */
  262.             type = max_weapons_avail;  /* set this so we will exit loop */ 
  263.           }
  264.  
  265.          if( com1 == 'E'|| com1 == 'e')     return(1);
  266.          if( com1 == 'C'|| com1 == 'c'){ clear_it(); 
  267.           not_complete = 0;}
  268.     
  269.       } while(not_complete);
  270.  
  271.     }/* end of if. */
  272.     /* end of module TEMPLE */
  273.    
  274. }
  275.  
  276. /******************************************/
  277.  display_wares(type,off,zz)
  278.    int type,off;
  279.    int *zz;  
  280.    {   
  281.       sprintf(string,"[%d] %s:%d",type,items[off].name,items[off].price);
  282.       /*v_justified(handle,6,*zz,string,184,0,1);*/
  283.     v_gtext(handle,6,*zz,string);
  284.       *zz+=8;
  285.      }
  286.  
  287. /*******************************************/
  288. process_the_buy(number,offset1,type_of_estab)
  289. int offset1,type_of_estab;
  290. int number;
  291. {
  292.   char com2;
  293.   int offset,really_wants_to_buy = 1;  /* initally TRUE, but if
  294.                                           in Armory, and you already
  295.                                           have some armour, its set
  296.                                           to False, and only way to
  297.                                           set back to True is if you
  298.                                           explicitly say Y or y! */
  299.  
  300.   offset = offset1 + number;
  301.   clear_it();
  302.     
  303.     if (type_of_estab == 1)      /* if in armory */
  304.     {
  305.        if ( (strcmp(user.armor,"None")) != 0 ) /* if you have armor already.. */
  306.         {
  307.          really_wants_to_buy = 0;      /* set to negative! */
  308.          v_gtext(handle,6,11,"You seem to already ");
  309.          v_gtext(handle,6,19,"have some armor. Are");
  310.          v_gtext(handle,6,27,"you sure you want to");
  311.          v_gtext(handle,6,35,"buy?   [y=n]        ");
  312.          com2 = Bconin(2);   
  313.          if ( com2 == 'Y' || com2 == 'y' ) really_wants_to_buy=1;
  314.         }
  315.     } 
  316.  
  317. if( really_wants_to_buy == 1)
  318.   {
  319.      v_justified(handle,6,11,"So you want to buy a ",184,0,1);
  320.      sprintf(string,"%s",items[offset].name);
  321.      v_gtext(handle,6,19,string);
  322.      sprintf(string,"It'll cost %d gp",items[offset].price);
  323.      v_justified(handle,6,27,string,184,0,1);
  324.      v_gtext(handle,6,35,"You still want it [y=n]");
  325.       com2 = Bconin(2);
  326.      if( com2 == 'Y' || com2 =='y')
  327.      {
  328.       if( ( user.gold - items[offset].price) < 0) 
  329.          v_justified(handle,6,43,"Not enough gold!!",184,0,1);
  330.       else
  331.        {
  332.  
  333.       if(type_of_estab == 0)
  334.       {
  335.         v_justified(handle,6,43,"Where do you want to",184,0,1);
  336.          v_gtext(handle,6,51,"place it [0-6]:");
  337.           do
  338.           {   
  339.            com2 = Bconin(2);
  340.           }while (  (com2 - '0') < 0 || (com2 - '0') > 6);
  341.           
  342.           if( user.backpack[com2 - '0'] != -1 )
  343.            {
  344.             clear_it();
  345.             v_gtext(handle,6,11,"You already have an ");
  346.             v_gtext(handle,6,19,"item in that space."); 
  347.            }         
  348.           else
  349.            {
  350.             user.backpack[com2 - '0'] = offset;
  351.             user.gold =user.gold -  items[offset].price;
  352.             v_gtext(handle,6,59,"Enjoy your");
  353.             sprintf(string,"%s!",items[offset].name);
  354.            v_gtext(handle,6,67,string);
  355.           }
  356.       } /* end of if_type of estab == 0 */
  357.     
  358.  
  359.  
  360.      if(type_of_estab == 2)  /* if supply */
  361.       {clear_it();
  362.        if(items[offset].price < user.gold)
  363.        {
  364.         switch(offset)
  365.          {
  366.            case 0: user.user_items[2]++;    break;  /* keys */
  367.            case 1: user.user_items[3]++;     break;  /* torches */
  368.            case 2: user.user_items[13]++;   break;  /* watch */
  369.          }
  370.          user.gold -=items[offset].price;
  371.         } 
  372.        else {
  373.             v_gtext(handle,6,11,"Sorry...you dont");
  374.             v_gtext(handle,6,19,"have the funds..");
  375.             }
  376.    }    /* end of supply buy.. */
  377.  
  378.  
  379.       if(type_of_estab == 1)                 /* yuor in an armory */
  380.        {
  381.          /* use another lookup item table for armor */
  382.          user.gold = user.gold - items[offset].price;
  383.          user.armor_num = offset;
  384.          user.ac = 18 - user.armor_num;   /* compute new ac */
  385.          strcpy(user.armor,items[offset].name);
  386.           v_gtext(handle,6,59,"Enjoy your");
  387.            sprintf(string,"%s!",items[offset].name);
  388.            v_gtext(handle,6,67,string);
  389.        }  
  390.           
  391.      }  /* end of else...*/
  392.  
  393.  
  394.  
  395.       }
  396.   } /* end of really_wants_to buy == 1 */
  397. }
  398.  
  399.  
  400.  
  401. /**********************************/
  402.  
  403. char *lookup_backpack(d)
  404. int d;
  405.   
  406.  {
  407.   int number;
  408.   
  409.    number = user.backpack[d];
  410.    if (number == -1) return ("Nothing");
  411.    return(unique_item[number].name);
  412.  
  413.  } 
  414.   
  415. /******************************/
  416. view_backpack()
  417. {
  418.  int zz = 27,d;
  419.  
  420.  clear_it();
  421.  v_gtext(handle,6,11,"Items in backpack");
  422.  v_gtext(handle,6,19,"*****************");
  423.   for(d=0;d<7;d++)
  424.   {
  425.    sprintf(string,"[%d] %s",d,lookup_backpack(d));
  426.       v_gtext(handle,6,zz,string);
  427.       zz+=8;
  428.   }
  429. }
  430. /***************************************************/
  431.